package com.czl.module_main.fragment

import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.module_main.BR
import com.czl.module_main.R
import com.czl.module_main.adapter.HouseKeeperAdapter
import com.czl.module_main.databinding.FragmentHousekeepersBinding
import com.czl.module_main.databinding.HeadHousekeepersBinding
import com.czl.module_main.viewmodel.HouseKeepersViewModel

@Route(path = AppConstants.Router.Main.F_HOUSEKEEPERS)
class HouseKeepersFragment: BaseFragment<FragmentHousekeepersBinding, HouseKeepersViewModel>() {
    private lateinit var mAdapter: HouseKeeperAdapter
    override fun initContentView(): Int {
        return R.layout.fragment_housekeepers
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("社区管家")
        reload()
        initAdapter()
    }

    override fun initViewObservable() {
        viewModel.loadCompleteEvent.observe(this, { data ->
//            if (inputString.isEmpty()){
            handleRecyclerviewData(
                data == null,
                data?.records as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                viewModel.currentPage,
                data?.current == data?.pages
            )
//            }else{
////                ^[\u4e00-\u9fa5_a-zA-Z0-9]+$
//                carAccessList?.clear()
//                for (item in data){
//                    if (item.deviceName.matches(Regex.fromLiteral(inputString))){
//                        carAccessList?.add(item)
//                    }
//                }
//                handleRecyclerviewData(
//                    carAccessList == null,
//                    carAccessList,
//                    mAdapter,
//                    binding.ryCommon,
//                    binding.smartCommon,
//                    1,
//                    true
//                )
//            }
        })
//        viewModel.inputChangeEvent.observe(this,{
//            inputString=it
//            var tempCarAcessList: ArrayList<CarAceessItemBean> = arrayListOf()
//            for (item in carAccessList!!){
//                if (item.deviceName.matches(Regex.fromLiteral(inputString))){
//                    tempCarAcessList.add(item)
//                }
//            }
//            mAdapter.setList(tempCarAcessList)
//        })

    }

    private fun initAdapter() {
        mAdapter = HouseKeeperAdapter(this)
        binding.etSearchContent.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                viewModel.searchName= p0.toString()
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })
        binding.ivKeeperSearch.setOnClickListener {
            binding.smartCommon.autoRefresh()
        }
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}